package com.miraclengine.http;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import com.miraclengine.utils.Debug;
import com.miraclengine.utils.GlobalStatic;

public class HttpClientHelper {
	
	private String TAG = HttpClientHelper.class.getSimpleName();
	
	private final int connectTimeout = 15 * 1000;
	private final int readTimeout = 15 * 1000;
	
	private static HttpClientHelper instance = null;
	
	private final static class InstanceHolder {
		final static HttpClientHelper SINGLE_INSTANCE = new HttpClientHelper();
	}
	
	private String encoding = "utf-8";
	
	public String getEncoding() {
		return encoding;
	}
	
	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	
	public static HttpClientHelper getInstance() {
		if(instance == null) {
			instance = InstanceHolder.SINGLE_INSTANCE;
		}
		return instance;
	}

	public String httpGet(String preUrl, ArrayList<NameValuePair> datas) {
		StringBuffer sb = new StringBuffer();
		StringBuffer url = new StringBuffer();
		
		url.append(preUrl);
		
		for(NameValuePair data : datas) {
			url.append(data.getName());
			url.append("=");
			url.append(URLEncoder.encode(data.getValue()));
			url.append("&");
		}
		
		Debug.v(TAG, "execute URL : " + url);
		
		HttpParams params = new BasicHttpParams();
		HttpConnectionParams.setSoTimeout(params, readTimeout);
		HttpConnectionParams.setConnectionTimeout(params, connectTimeout);
		HttpConnectionParams.setTcpNoDelay(params, true);
		
		HttpClient httpClient = new DefaultHttpClient(params);
		
		try {
			HttpGet method = new HttpGet(url.toString());
			HttpResponse response = httpClient.execute(method);
			
			InputStream is = response.getEntity().getContent();
			InputStreamReader isr = new InputStreamReader(is, encoding);
			BufferedReader br = new BufferedReader(isr);
			
			String line = null;
			
			while((line = br.readLine()) != null) {
				sb.append(line + "\n");
			}
			
			Debug.d(TAG, "get response : " + sb.toString());
		} catch(Exception e) {
			Debug.d(TAG, "httpGet : " + e.getMessage());
			return null;
		}
		
		return sb.toString();
	}

	public String httpPost(String preUrl, ArrayList<NameValuePair> datas) {
		if(GlobalStatic.DEBUG_MODE) {
			Debug.i(TAG, "Debug to Post HttpConnection ----------------------");
			for(int i=0 ; i<datas.size() ; i++) {
				Debug.i(TAG, datas.get(i).getName() + " = " + datas.get(i).getValue());
			}
		}
		
		HttpParams params = new BasicHttpParams();
		HttpConnectionParams.setSoTimeout(params, readTimeout);
		HttpConnectionParams.setConnectionTimeout(params, connectTimeout);
		HttpConnectionParams.setTcpNoDelay(params, true);
		
		HttpClient httpClient = new DefaultHttpClient(params);
		HttpPost method = new HttpPost(preUrl);
		StringBuffer sb = new StringBuffer();
		
		try {
			method.setEntity(new UrlEncodedFormEntity(datas));
			HttpResponse response = httpClient.execute(method);
			
			InputStream is = response.getEntity().getContent();
			InputStreamReader isr = new InputStreamReader(is, encoding);
			BufferedReader br = new BufferedReader(isr);
			
			String line = null;
			
			while((line = br.readLine()) != null) {
				sb.append(line + "\n");
			}
			
			Debug.d(TAG, "post response : " + sb.toString());
		} catch(Exception e) {
			Debug.d(TAG, "httpPost : " + e.getMessage());
		}
		
		return sb.toString();
	}
}
